/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pe.basic.controller;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import org.apache.log4j.Logger;

import com.pe.basic.util.Constantes;
import com.pe.basic.util.MessageProvider;

/**
 *
 * @author WCCAICEDO
 */

@SessionScoped
@ManagedBean
public class AccesoController {
    
    private static final Logger log = Logger.getLogger(AccesoController.class);
    
    private String usuario;
    private String password;
    private String messageErrorLogin;
    
	public void onPageLoad(ComponentSystemEvent event) {
        try {
            if (!FacesContext.getCurrentInstance().isPostback()) {
                setUsuario(null);
                setPassword(null);
                setMessageErrorLogin(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    MessageProvider.getInstance().getValue("MSG_ERROR_TITLE_GENERAL"),
                    MessageProvider.getInstance().getValue("MSG_ERROR_DESCRIP_GENERAL")));
        }
    }

    /**
     * Metodo de Logueo
     * @return
     */
    public String login() {
        try {
        	log.info("El usuario es: " + usuario + " , y el password es: " + password);
        	
            if (usuario.equals(Constantes.USER) && password.equals(Constantes.PASSWORD)) {
            	log.info("Se loguea!");
                return "principal.xhtml?faces-redirect=true";
            } else {
				messageErrorLogin = MessageProvider.getInstance().getValue("MSG_ERROR_LOGIN_USER");
                return "login.xhtml?faces-redirect=false";
            }
            
        } catch (Exception e) {
            e.printStackTrace();
			log.error(e.getMessage(),e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    MessageProvider.getInstance().getValue("MSG_ERROR_TITLE_GENERAL"),
                    MessageProvider.getInstance().getValue("MSG_ERROR_DESCRIP_GENERAL")));
            return "login.xhtml?faces-redirect=false";
        }
    }
    
    
    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the messageErrorLogin
     */
    public String getMessageErrorLogin() {
        return messageErrorLogin;
    }

    /**
     * @param messageErrorLogin the messageErrorLogin to set
     */
    public void setMessageErrorLogin(String messageErrorLogin) {
        this.messageErrorLogin = messageErrorLogin;
    }
    
    
}
